package com.mini.cn.goods.lottery;

import java.util.HashMap;
import java.util.List;

public class LotteryManager {

    //人数定额抽奖
    private  PeopleLottery peopleLottery;

    //转盘抽奖
    private TurningLottery turningLottery;

    //主持人抽奖
    private ProgrammeLottery  programmeLottery;


    private static volatile LotteryManager lotteryManager;

    private final static byte[] lock=new byte[0];

    private LotteryManager(){
        peopleLottery=new PeopleLottery();
        turningLottery=new TurningLottery();
    }

    public static LotteryManager getIntanceLotteryManager(){
        if(lotteryManager==null){
            synchronized (lock){
                if(lotteryManager==null) {
                    lotteryManager = new LotteryManager();
                }
            }
        }
        return lotteryManager;
    }

    /**
     *
     * @param prizes 奖品
     * @param number 抽奖池子池子大小 与概率有关
     * @return
     */
    public PeopleLottery createPeopleLottery(List<Prize> prizes, int number,int appId){
        if(peopleLottery.prizes!=null){
            peopleLottery.prizes.clear();
            peopleLottery.prizes=null;
        }
        peopleLottery.prizes=prizes;
        peopleLottery.peopleNumber=number;
        return peopleLottery;
    }

    /**
     * 获取剩余奖品
     * @return
     */
    public int getLeftNumberPrice(int appid){
        if(peopleLottery!=null){
            return peopleLottery.peopleNumber;
        }
        return 0;
    }

    /**
     * 抽奖
     * @return
     */
    public Prize peopleLottery(int appId){
        Prize prize=null;
        if(peopleLottery!=null){
            prize= peopleLottery.LotteryPrize();
        }
        return prize;
    }

    /**
     * 生成转盘抽奖
     * @param prizes
     * @return
     */
    public TurningLottery createTurningLottery(List<Prize> prizes,int appId){
        if(turningLottery.prizes!=null){
            turningLottery.prizes.clear();
            turningLottery.prizes=null;
        }
        turningLottery.prizes=prizes;
        turningLottery.creatLottoeyBox();
        return turningLottery;
    }

    /**
     * 转盘抽奖
     * @return
     */

    public Prize turnLottery(){
        Prize prize=null;
        if(turningLottery!=null){
            prize=turningLottery.LotteryPrize();
        }
        return prize;
    }


    /**
     * 生成主持人抽奖
     * @param peopleArea
     * @return
     */
    public ProgrammeLottery createProgrammLottery(HashMap<String,List<Integer>> peopleArea){
        if(programmeLottery.peopleArea!=null){
            programmeLottery.peopleArea.clear();
            programmeLottery.peopleArea=null;
        }
        programmeLottery.peopleArea=peopleArea;
        programmeLottery.creatLottoeyBox();
        return programmeLottery;
    }

    /**
     * 主持人抽奖
     * @return
     */

    public String programmLottery(){
        String key=null;
        if(programmeLottery!=null){
             key=programmeLottery.LotteryPrize();
        }
        return key;
    }


    public void  clear(){
        if(peopleLottery!=null){
            peopleLottery.clearLotteryBuffer();
        }
        if(turningLottery!=null){
            turningLottery.clearLotteryBuffer();
        }
        if(programmeLottery!=null){
            programmeLottery.clearLotteryBuffer();
        }
    }
}
